package com.mall.db.api;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.db.mapper.SmsFlashPromotionMapper;
import com.mall.pojo.SmsFlashPromotion;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.Serializable;
import java.util.List;

@DubboService
public class SmsFlashPromotionApiImpl implements SmsFlashPromotionApi {

    @Autowired
    private SmsFlashPromotionMapper promotionMapper;

    /**
     * 根据条件分页查询秒杀活动
     *
     * @param pageNum
     * @param pageSize
     * @param keyword  不必须
     * @return
     */
    @Override
    public Page<SmsFlashPromotion> findByPageAndCondition(Integer pageNum, Integer pageSize, String keyword) {
        Page<SmsFlashPromotion> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<SmsFlashPromotion> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StrUtil.isNotEmpty(keyword), SmsFlashPromotion::getTitle, keyword);
        return promotionMapper.selectPage(page, wrapper);
    }

    /**
     * 编辑活动信息
     * @param flashPromotion
     */
    @Override
    public Integer update(SmsFlashPromotion flashPromotion) {
        int count = promotionMapper.updateById(flashPromotion);
        return count;
    }

    /**
     * 添加活动信息
     * @param flashPromotion
     */
    @Override
    public Integer create(SmsFlashPromotion flashPromotion) {
        int count = promotionMapper.insert(flashPromotion);
        return count;
    }

    /**
     * 根据id删除活动信息
     * @param id
     */
    @Override
    public Integer delete(Integer id) {
        int count = promotionMapper.deleteById(id);
        return count;
    }

    /**
     * 根据id查询活动信息
     * @param id
     * @return
     */
    @Override
    public SmsFlashPromotion findById(Integer id) {
        return promotionMapper.selectById(id);
    }
}
